﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using JESAI.Application.Abstractions.Admin;
using JESAI.Application.Abstractions.Dtos;
using JESAI.Application.Abstractions.Dtos.Menu;
using JESAI.Core;
using JESAI.Core.Extensions;
using JESAI.Framework.Filters;
using SqlSugar;
using System;
using JESAI.Core.Util.Collections;
using System.Linq;
using JESAI.Application.Abstractions.Admin.Dtos.Menu;

namespace Bucket.Admin.Web.Controllers
{
    /// <summary>
    /// 菜单管理
    /// </summary>
    [ApiController]
    [Authorize("permission")]
    [IgnoreResultHandlerAttribute]
    public class MenuController : ControllerBase
    {
        private readonly IMenuService _menuService;
        private readonly IUser _user;
        private readonly IPlatformService _platformService;
        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="sqlSugarDbContextFactory"></param>
        /// <param name="mapper"></param>
        /// <param name="user"></param>
        public MenuController(IMenuService menuService, IUser user, IPlatformService platformService)
        {
            _menuService = menuService;
            _user = user;
            _platformService = platformService;
        }
        /// <summary>
        /// 查询平台菜单
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("/Menu/QueryAllMenus")]
        public BaseOutput<object> QueryAllMenus([FromQuery] QueryAllMenusInput input)
        {
            var list = _menuService.QueryAllMenus(input);
            return new BaseOutput<object> { Data = list };
        }

        /// <summary>
        /// 查询平台菜单
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("/Menu/QueryAllMenuTrees")]
        public BaseOutput<object> QueryAllMenuTrees([FromQuery] QueryAllMenusInput input)
        {
            var list = _menuService.QueryAllMenuTrees(input);
            var treeList = list.ToTreeList(p => p.ParentId, p => p.Id, p => p.Children);
            return new BaseOutput<object> { Data = treeList };
        }

        /// <summary>
        /// 设置平台菜单信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/Menu/SetPlatform")]
        public BaseOutput SetPlatform([FromBody] SetMenuInput input)
        {
            _menuService.UpdateOrInsert(input);

            return new BaseOutput { };
        }
        /// <summary>
        /// 查询用户菜单
        /// </summary>
        /// <returns></returns>
        [HttpGet("/Menu/QueryUserMenus")]
        public BaseOutput<object> QueryUserMenus([FromQuery] QueryUserMenuInput input)
        {
            var userId = _user.Id.ToLong();
            var platformId = 0;
            if (!input.PlatformKey.IsEmpty())
            {
                var platformInfo = _platformService.QueryListByPlatformKey(input.PlatformKey);
                if (platformInfo != null)
                    platformId = platformInfo.Id;
            }
            var list = _menuService.QueryMenusByPlatformIdAndUserId(platformId, userId);
            var platformIdList = list.GroupBy(p => p.PlatformId).Select(it => it.First().PlatformId).ToList();
            var pidArr = platformIdList.ToArray();
            var platformList = _platformService.QueryPlatformListByIds(pidArr);
            return new BaseOutput<object> { Data = new { Menu = list, Platform = platformList } };
        }

        /// <summary>
        /// 查询页面的按钮
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("/Menu/QueryUserPageBtns")]
        public BaseOutput<List<MenuDto>> QueryUserPageBtns([FromQuery] QueryUserPageBtnInput input)
        {
            var list = _menuService.QueryUserPageBtns(input);
            return new BaseOutput<List<MenuDto>> { Data = list };
        }

        /// <summary>
        /// 查询页面的Link按钮
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("/Menu/QueryUserPageLinkBtns")]
        public BaseOutput<List<MenuDto>> QueryUserPageLinkBtns([FromQuery] QueryUserPageBtnInput input)
        {
            var list = _menuService.QueryUserPageLinkBtns(input);
            return new BaseOutput<List<MenuDto>> { Data = list };
        }
    }
}
